/*
 * @Author: your name
 * @Date: 2021-08-31 18:54:12
 * @LastEditTime: 2021-08-31 20:05:16
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \briup\Web课后练习成果\ES5\对象排序.js
 */
// 新建Teacher对象四个并放到数组中，
// 封装一个函数ascProperty，
// 能按照Teacher对象的属性进行正序排序，例如按照id排序、name排序、salary排序等。


//新建teacher对象
var teachers = [
    { name: 'zs', id: 22, salary: 10000 },
    { name: 'ls', id: 06, salary: 5000 },
    { name: 'ww', id: 03, salary: 3324 },
    { name: 'jia', id: 08, salary: 7000 }
];
//封装函数
function ascProperty(item) {
    return function(t1, t2) {
        if (t1[item] > t2[item]) {
            return 1;
        } else return -1;
    };
}
teachers.sort(ascProperty('salary'));
console.log(teachers);
// [
// { name: 'ww', id: 3, salary: 3324 },
// { name: 'ls', id: 6, salary: 5000 },
// { name: 'jia', id: 8, salary: 7000 },
// { name: 'zs', id: 22, salary: 10000 }
// ] 
teachers.sort(ascProperty("id"));
console.log(teachers);
// [
//     { name: 'ww', id: 3, salary: 3324 },
//     { name: 'ls', id: 6, salary: 5000 },
//     { name: 'jia', id: 8, salary: 7000 },
//     { name: 'zs', id: 22, salary: 10000 }
//   ]
teachers.sort(ascProperty("name"));
console.log(teachers);
// [
//     { name: 'jia', id: 8, salary: 7000 },
//     { name: 'ls', id: 6, salary: 5000 },
//     { name: 'ww', id: 3, salary: 3324 },
//     { name: 'zs', id: 22, salary: 10000 }
//   ]

// var teachers = [
//     { id: 3, name: "winnie", salary: 456 },
//     { id: 7, name: "briup", salary: 123 },
//     { id: 4, name: "jack", salary: 222 },
//     { id: 9, name: "rose", salary: 777 }
// ];
// var ascProperty = function(pro) {
//     return function(t1, t2) {
//         if (t1[pro] > t2[pro]) return 1;
//         else return -1;
//     };
// }
// teachers.sort(ascProperty("id"));
// console.log(teachers);
// teachers.sort(ascProperty("name"));
// console.log(teachers);
// teachers.sort(ascProperty("salary"));
// console.log(teachers);